//
//  NSString+Size.m
//  PFBrainTwister
//
//  Created by perfay-zjz on 2018/4/9.
//  Copyright © 2018年 perfayMini. All rights reserved.
//

#import "NSString+Size.h"

@implementation NSString (Size)

#pragma mark - 根据字符串计算label高度
- (CGFloat)getHeightWithW:(CGFloat)width withFont:(UIFont *)font {
    return [self getSizeWH:width withFont:font widthOrHeight:YES];
}

/*
 计算文本大小
 wOrHSize   固定宽度或者高度
 isHeight   是否计算高度 yes 计算高度 no计算宽度
 */
- (CGFloat)getSizeWH:(CGFloat)wOrHSize withFont:(UIFont *)font widthOrHeight:(BOOL)isHeight{
    CGSize size = CGSizeMake(0, 0);
    if (isHeight) {
        size = CGSizeMake(wOrHSize, CGFLOAT_MAX);
    }else
    {
        size = CGSizeMake(CGFLOAT_MAX, wOrHSize);
    }
    
    NSDictionary *dic = nil;
    if (!font) {
        dic =@{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    }else
    {
        dic =@{NSFontAttributeName:font};
    }
    CGSize contentSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    return isHeight==YES?contentSize.height:contentSize.width;
}
    
- (NSString*)getSubStingFromString:(NSString*)fromString toString:(NSString*)toString
{
    NSRange fromRange = [self rangeOfString:fromString];//匹配得到的下标
    NSRange toRange = [self rangeOfString:toString];//匹配得到的下标
    if (fromRange.length==NSNotFound || toString.length==NSNotFound) {
        return @"";
    }
    NSRange neewRange = NSMakeRange(fromRange.location+fromRange.length, toRange.location-fromRange.location-fromRange.length);
    return [self substringWithRange:neewRange];//截取范围内的字符串
}

- (NSString*)getSubStingtoString:(NSString*)toString
{
    NSRange toRange = [self rangeOfString:toString];
    if (toString.length==NSNotFound) {
        return self;
    }
    NSRange neewRange = NSMakeRange(0, toRange.location);
    return [self substringWithRange:neewRange];//截取范围内的字符串
}
                         

@end
